using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Point {
	
	public int x, y, z;
	public Point parent;
	public bool occupied;
	public int timesTried;
	
	public Point(int nx, int ny, int nz){
		x = nx;
		y = ny;
		z = nz;
		parent = null;
	}
	
	public Point(int nx, int ny, int nz, Point nparent){
		x = nx;
		y = ny;
		z = nz;
		parent = nparent;
	}
	
	public int getFCost(Point endPoint){
		int fCost = 0;
		fCost = getMovementCost() + getHeuristicCost(endPoint);
		return fCost;
	}
	
	public int getMovementCost(){
		int cost = 0;
		
		if (parent == null){
			return cost;	
		} else {
			if (parent.x != x){
				if (parent.y != y){
					if (parent.z != z){
						cost = 17;
					} else {
						cost = 14;
					}
				} else {
					if (parent.z != z){
						cost = 14;
					} else {
						cost = 10;
					}
				}
			} else {
				if (parent.y != y){
					if (parent.z != z){
						cost = 14;
					} else {
						cost = 10;
					}
				} else {
					if (parent.z != z){
						cost = 10;
					} else {
						cost = 10;
					}
				}
			}
		}
		
		return cost;
	}
	
	public int getHeuristicCost(Point endPoint){
		int cost = 0;
		
		int distanceX = Mathf.Abs(endPoint.x - this.x);
		int distanceY = Mathf.Abs(endPoint.y - this.y);
		int distanceZ = Mathf.Abs(endPoint.z - this.z);
		
		cost = (distanceX + distanceY + distanceZ) * 13;
		
		return cost;
	}
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public bool isEqualTo(Point point){
		if ((this.x == point.x) & (this.y == point.y) & (this.z == point.z)){
			return true;	
		} else {
			return false;
		}
	}
	
	public Point[] getAdjacentPoints(){
		Point[] adjacentPoints = {new Point(x+1, y, z, this), new Point(x+1, y+1, z, this), 
			new Point(x+1, y-1, z, this), new Point(x-1, y, z, this), new Point(x-1, y+1, z, this), 
			new Point(x-1, y-1, z, this), new Point(x, y-1, z, this), new Point(x, y+1, z, this)
		};
		return adjacentPoints;
	}
	
	public Point[] getAdjacentPoints3d(){
		List<Point> adjacentPoints = new List<Point>();
		
		for (int nx = -1; nx <= 1; nx ++){
			for (int ny = -1; ny <= 1; ny ++){
				for(int nz = -1; nz <= 1; nz ++){
					if ((nx != 0) | (ny != 0) | (nz != 0)){
						adjacentPoints.Add(new Point(x + nx, y + ny, z + nz));
					}
				}
			}
		}
		
		Point[] arr = (Point[])adjacentPoints.ToArray();
		
		return arr;
	}
	
	
}
